


## Load packages and data
pacman::p_load(fixest, tidyverse, broom, data.table)


df <-  readRDS("Data/Regimedata/Measures_merged_covariates.rds")




# normalize cagenmob and physical violence measure


df$v2cagenmob_nor <- (df$v2cagenmob - min(df$v2cagenmob, na.rm = T)) / (max(df$v2cagenmob, na.rm = T) - min(df$v2cagenmob, na.rm = T))

df$v2x_clphy*-1 
df$v2x_clphy_nor <- (df$v2x_clphy - min(df$v2x_clphy, na.rm = T)) / (max(df$v2x_clphy, na.rm = T) - min(df$v2x_clphy, na.rm = T)) 

summary(df$v2cagenmob_nor)
summary(df$v2x_clphy_nor)

##select dependent and independent variables

depvars <- c("reg_breakdown", "onset1", "v2x_clphy_nor", "v2cagenmob_nor", "coup_attempt")

names(df)

indepvars <- c("RegType_lied_CA + RegType_lied_DEM",
               "RegType_RoW_CA + RegType_RoW_DEM",
               "Politytype_Autocracy + Politytype_Democracy",
               "status_fh_NF + status_fh_F",
               "HTW_RegType_Non_MP_Autocracy + HTW_RegType_Democracy",
               "RegType_magaloni_CA + RegType_magaloni_DEM",
               "AnckarRegtype_Non_MP_Autocracy + AnckarRegtype_Democracy")



# Create a list of all possible combinations of depvars and indepvars
combinations <- expand.grid(depvars, indepvars)

combinations

## Prepare df with common sample frame

df2 <- df %>% 
  select(RegType_lied_EA, RegType_RoW_EA,  Politytype_Anocracy, status_fh_PF,
         HTW_RegType_MP_Autocracy, RegType_magaloni_EA, AnckarRegtype_MP_Autocracy, year, Final_Code) %>% 
  na.omit() %>%
  select(year, Final_Code) %>%
  left_join(df, by = c("year", "Final_Code"))

names(df2)

## run all model combinations
results <- apply(combinations, 1, function(x) {
  depvar <- x[1]
  indepvar <- x[2]
  
  list(
  feols(as.formula(paste(depvar, "~", indepvar)), cluster = "Final_Code", data = df) %>% tidy() %>%
    mutate(FE  = "No FE", Sample = "Full Samples", Outcome = depvar ),
  feols(as.formula(paste(depvar, "~", indepvar, " | Final_Code + year")), cluster = "Final_Code", data = df) %>%tidy()%>%
    mutate(FE  = "Country & Year", Sample = "Full Samples", Outcome = depvar),
  feols(as.formula(paste(depvar, "~", indepvar)), cluster = "Final_Code", data = df2) %>%tidy() %>%
    mutate(FE  = "No FE", Sample = "Overlapping Sample", Outcome = depvar),
  feols(as.formula(paste(depvar, "~", indepvar, " | Final_Code + year")), cluster = "Final_Code", data = df2) %>%tidy() %>%
    mutate(FE  = "Country & Year", Sample = "Overlapping Sample", Outcome = depvar)) %>% rbindlist()
  
  
  })


## combine models in df



coefplotframe <- results %>% 
  rbindlist %>% 
  as.data.frame %>% filter ( term != "(Intercept)" ) %>% mutate ( conf.low_95 = estimate - 1.96 *  std.error, conf.high_95 = estimate + 1.96 * std.error,
                                                                conf.low_90 = estimate - 1.64 *  std.error, conf.high_90 = estimate + 1.64 * std.error)


coefplotframe$Regime <- rep(c("Other Autocracy", "Democracy"), nrow(coefplotframe)/2)

coefplotframe$Indicator <- c(rep("LIED", 40),rep("RoW", 40), rep("Polity", 40),  rep("FH", 40),  rep("ARD", 40),  rep("AoW", 40),  rep("CPR", 40) )





coefplotframe$Indicator <- fct_rev(factor(coefplotframe$Indicator, levels=c("LIED", "RoW", "Miller", "Polity", "FH", "ARD", "AoW", "CPR")))


coefplotframe$Outcome <- as.factor(coefplotframe$Outcome)

levels(coefplotframe$Outcome) <- c("Coup attempt", "Intrastate conflict", "Regime breakdown", "Mass mobilization", "Repression")




coefplotframe <- coefplotframe %>% 
  filter(Indicator != "Miller")

coefplotframe$Indicator <- as.factor(as.character(coefplotframe$Indicator))


levels(coefplotframe$Indicator) <- c("Authoritarian Regimes Data Set", "Autocracies of the World", "Classifying Political Regimes", "Freedom House", "Lexical Index of Electoral Democracy", "Polity", "Regimes of the World")

coefplotframe$Indicator <- factor(coefplotframe$Indicator, levels=c("Lexical Index of Electoral Democracy", "Regimes of the World", "Polity", "Freedom House", "Authoritarian Regimes Data Set", "Autocracies of the World", "Classifying Political Regimes"))


### Make Figure 5

coefplotframe %>% filter(FE == "Country & Year" ) %>% 
  ggplot(aes(x=Regime, y = estimate, shape = fct_rev(Indicator))) +
  geom_hline(yintercept = 0, 
             colour = gray(1/2), lty = 2) +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_95,
                     ymax = conf.high_95), position = position_dodge(width = .75), alpha = .7, color = "grey40") +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_90,
                     ymax = conf.high_90), position = position_dodge(width = .75), linewidth = 1, alpha = .7, color = "grey40")   +
  geom_point(aes(x = Regime, 
                 y = estimate), size = 4, position = position_dodge(width = .75), color = "black") +
  ggtitle("") + ylab ("") + xlab("") +  guides(color = guide_legend(reverse=TRUE, ncol = 2, title = ""), shape = guide_legend(reverse=TRUE, ncol = 2, title = "")) + 
  coord_flip()  + scale_y_continuous(breaks = 0) +
  theme_classic(base_size = 20) + facet_grid(vars(fct_rev(Sample)), vars(fct_rev(Outcome)), scales = "free_x") +
  labs(colour="Measure", shape = "Measure")  + scale_color_grey() +
  scale_shape_manual(values = c(55:49))+ theme(legend.position="bottom")



## Make Figure H1

coefplotframe %>% filter(FE == "No FE" ) %>% 
  ggplot(aes(x=Regime, y = estimate, shape = fct_rev(Indicator))) +
  geom_hline(yintercept = 0, 
             colour = gray(1/2), lty = 2) +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_95,
                     ymax = conf.high_95), position = position_dodge(width = .75), alpha = .7, color = "grey40") +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_90,
                     ymax = conf.high_90), position = position_dodge(width = .75), linewidth = 1, alpha = .7, color = "grey40")   +
  geom_point(aes(x = Regime, 
                 y = estimate), size = 4, position = position_dodge(width = .75), color = "black") +
  ggtitle("") + ylab ("") + xlab("") + guides(color = guide_legend(reverse=TRUE, ncol = 2, title = ""), shape = guide_legend(reverse=TRUE, ncol = 2, title = "")) + 
  coord_flip()  + scale_y_continuous(breaks = 0) +
  theme_classic(base_size = 20) + facet_grid(vars(fct_rev(Sample)), vars(fct_rev(Outcome)), scales = "free_x") +
  labs(colour="Measure", shape = "Measure")  + scale_color_grey() +
  scale_shape_manual(values = c(55:49))+ theme(legend.position="bottom")





##Figure I1


coefplotframe %>% filter(FE == "Country & Year" ) %>% 
  ggplot(aes(x=Regime, y = estimate, shape = fct_rev(Indicator))) +
  geom_hline(yintercept = 0, 
             colour = gray(1/2), lty = 2) +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_95,
                     ymax = conf.high_95), position = position_dodge(width = .75), alpha = .7, color = "grey40") +
  geom_linerange(aes(x = Regime, 
                     ymin = conf.low_90,
                     ymax = conf.high_90), position = position_dodge(width = .75), linewidth = 1, alpha = .7, color = "grey40")   +
  geom_point(aes(x = Regime, 
                 y = estimate), size = 4, position = position_dodge(width = .75), color = "black") +
  ggtitle("") + ylab ("") + xlab("") + guides(color = guide_legend(reverse=TRUE, ncol = 2, title = ""), shape = guide_legend(reverse=TRUE, ncol = 2, title = "")) + 
  coord_flip()  + scale_y_continuous() +
  theme_classic(base_size = 20) + facet_grid(vars(fct_rev(Sample)), vars(fct_rev(Outcome))) +
  labs(colour="Measure", shape = "Measure")  + scale_color_grey() +
  scale_shape_manual(values = c(55:49))+ theme(legend.position="bottom")











